﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GatewayInspect.Common;

namespace GatewayInspect.Modules
{
    public partial class FKModifyForm : PopupForm
    {
        private string _sendCardTime = "";

        public FKModifyForm()
        {
            InitializeComponent();
        }

        public FKModifyForm(string _truckNo, string _cardNo, string _weight, string _time)
            : this()
        {
            this.tbxTruckNo.Text = _truckNo;
            this.tbxCardNo.Text = _cardNo;
            this.tbxHDWeight.Text = _weight;
            this.tbxNewTruckNo.Text = _truckNo;//默认值
            this.tbxNewHDWeight.Text = _weight;//默认值
            this._sendCardTime = _time;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tbxNewTruckNo.Text))
            {
                MessageBox.Show("新车牌号不能为空！");
                return;
            }
            if (tbxNewTruckNo.Text.Trim().Length != 7)
            {
                MessageBox.Show("新车牌号位数不正确！");
                return;
            }
            double _hdWeight = 0;
            if (!string.IsNullOrWhiteSpace(tbxNewHDWeight.Text) && !double.TryParse(tbxNewHDWeight.Text.Trim(), out _hdWeight))
            {
                MessageBox.Show("车辆载质量非法！");
                return;
            }
            string strTruckNo = tbxNewTruckNo.Text.Trim().ToUpper();
            if (strTruckNo != tbxTruckNo.Text.Trim().ToUpper())//修改了卡号
            {
                DataTable dtRFID = DataAccess.ExecuteDataTable("select CARDID from RFID_READ where CARDSTATE = '0' and CHEHAO = '" + strTruckNo + "'");
                if (dtRFID != null && dtRFID.Rows.Count > 0)
                {
                    MessageBox.Show("该车已经发卡，卡号：" + dtRFID.Rows[0]["CARDID"].ToString());
                    return;
                }
            }
            string strCarID = tbxCardNo.Text;
            string strSql = "insert into RFID_READ_LOG(CARDID, CHEHAO, SENDCARDTIME, SENDCARDMAN, UPDATEMAN, UPDATEDATE, CHEHAO_NEW, HD_WEIGHT, HD_WEIGHT_NEW, ORGCODE)" +
                " select CARDID,CHEHAO,SENDCARDTIME,SENDCARDMAN,'" + Session.CurrentUserName + "',SYSDATE,'" + strTruckNo + "',HD_WEIGHT, '" + _hdWeight + "', ORGCODE from RFID_READ" +
                " where decode(ORGCODE,'1','01',ORGCODE) = '" + Session.CurrentOrgCode + "' and CARDID='" + strCarID + "' and SENDCARDTIME=to_date('" + _sendCardTime + "','yyyy-mm-dd hh24:mi:ss')";
            DataAccess.ExecuteNonQuery(strSql);
            strSql = "update RFID_READ set CHEHAO='" + strTruckNo + "', HD_WEIGHT='" + _hdWeight + "' where decode(ORGCODE,'1','01',ORGCODE) = '" +
                Session.CurrentOrgCode + "' and CARDID='" + strCarID + "' and SENDCARDTIME=to_date('" + _sendCardTime + "','yyyy-mm-dd hh24:mi:ss')";
            DataAccess.ExecuteNonQuery(strSql);

            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
